{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<div style=\"color:#777777;background-color:#ffffff;font-size:12px;text-align:right;\">\n",
    "\tprepared by Abuzer Yakaryilmaz (QuSoft@Riga) | November 07, 2018\n",
    "</div>\n",
    "<table><tr><td><i> I have some macros here. If there is a problem with displaying mathematical formulas, please run me to load these macros.</i></td></td></table>\n",
    "$ \\newcommand{\\bra}[1]{\\langle #1|} $\n",
    "$ \\newcommand{\\ket}[1]{|#1\\rangle} $\n",
    "$ \\newcommand{\\braket}[2]{\\langle #1|#2\\rangle} $\n",
    "$ \\newcommand{\\inner}[2]{\\langle #1,#2\\rangle} $\n",
    "$ \\newcommand{\\biginner}[2]{\\left\\langle #1,#2\\right\\rangle} $\n",
    "$ \\newcommand{\\mymatrix}[2]{\\left( \\begin{array}{#1} #2\\end{array} \\right)} $\n",
    "$ \\newcommand{\\myvector}[1]{\\mymatrix{c}{#1}} $\n",
    "$ \\newcommand{\\myrvector}[1]{\\mymatrix{r}{#1}} $\n",
    "$ \\newcommand{\\mypar}[1]{\\left( #1 \\right)} $\n",
    "$ \\newcommand{\\mybigpar}[1]{ \\Big( #1 \\Big)} $\n",
    "$ \\newcommand{\\sqrttwo}{\\frac{1}{\\sqrt{2}}} $\n",
    "$ \\newcommand{\\dsqrttwo}{\\dfrac{1}{\\sqrt{2}}} $\n",
    "$ \\newcommand{\\onehalf}{\\frac{1}{2}} $\n",
    "$ \\newcommand{\\donehalf}{\\dfrac{1}{2}} $\n",
    "$ \\newcommand{\\hadamard}{ \\mymatrix{rr}{ \\sqrttwo & \\sqrttwo \\\\ \\sqrttwo & -\\sqrttwo }} $\n",
    "$ \\newcommand{\\vzero}{\\myvector{1\\\\0}} $\n",
    "$ \\newcommand{\\vone}{\\myvector{0\\\\1}} $\n",
    "$ \\newcommand{\\vhadamardzero}{\\myvector{ \\sqrttwo \\\\  \\sqrttwo } } $\n",
    "$ \\newcommand{\\vhadamardone}{ \\myrvector{ \\sqrttwo \\\\ -\\sqrttwo } } $\n",
    "$ \\newcommand{\\myarray}[2]{ \\begin{array}{#1}#2\\end{array}} $\n",
    "$ \\newcommand{\\X}{ \\mymatrix{cc}{0 & 1 \\\\ 1 & 0}  } $\n",
    "$ \\newcommand{\\Z}{ \\mymatrix{rr}{1 & 0 \\\\ 0 & -1}  } $\n",
    "$ \\newcommand{\\Htwo}{ \\mymatrix{rrrr}{ \\frac{1}{2} & \\frac{1}{2} & \\frac{1}{2} & \\frac{1}{2} \\\\ \\frac{1}{2} & -\\frac{1}{2} & \\frac{1}{2} & -\\frac{1}{2} \\\\ \\frac{1}{2} & \\frac{1}{2} & -\\frac{1}{2} & -\\frac{1}{2} \\\\ \\frac{1}{2} & -\\frac{1}{2} & -\\frac{1}{2} & \\frac{1}{2} } } $\n",
    "$ \\newcommand{\\CNOT}{ \\mymatrix{cccc}{1 & 0 & 0 & 0 \\\\ 0 & 1 & 0 & 0 \\\\ 0 & 0 & 0 & 1 \\\\ 0 & 0 & 1 & 0} } $\n",
    "$ \\newcommand{\\norm}[1]{ \\left\\lVert #1 \\right\\rVert } $"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h2> <font color=\"blue\"> Solutions for </font>Quantum State</h2>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a id=\"task1\"></a>\n",
    "<h3> Task 1 </h3>\n",
    "\n",
    "Let $a$ and $b$ be real numbers.\n",
    "\n",
    "If the folllowing vectors are valid quantum states, then what can be the values of $a$ and $b$?\n",
    "\n",
    "$$\n",
    "    \\ket{v} = \\myrvector{a \\\\ -0.1 \\\\ -0.3 \\\\ 0.4 \\\\ 0.5}\n",
    "    ~~~~~ \\mbox{and} ~~~~~\n",
    "    \\ket{u} = \\myrvector{ \\frac{1}{\\sqrt{2}} \\\\ \\frac{1}{\\sqrt{b}} \\\\ -\\frac{1}{\\sqrt{3}} }.\n",
    "$$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3> Solution </h3>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# vector |v>\n",
    "\n",
    "print(\"vector |v>\")\n",
    "\n",
    "values = [-0.1, -0.3, 0.4, 0.5]\n",
    "\n",
    "total = 0 # summation of squares\n",
    "for i in range(len(values)):\n",
    "    total += values[i]**2; # add the square of each value\n",
    "print(\"total is \",total)\n",
    "print(\"the missing part is\",1-total)\n",
    "print(\"so, the value a should be\",(1-total)**0.5) # sqaure root of the missing part\n",
    "\n",
    "print()\n",
    "print(\"vector |u>\")\n",
    "\n",
    "values = [1/(2**0.5), -1/(3**0.5)]\n",
    "\n",
    "total = 0 # summation of squares\n",
    "for i in range(len(values)):\n",
    "    total += values[i]**2; # add the square of each value\n",
    "print(\"total is \",total)\n",
    "print(\"the missing part is\",1-total)\n",
    "# the missing part is 1/b, square of 1/sqrt(b)\n",
    "# thus b is 1/missing-part\n",
    "print(\"so, the value b should be\",1/(1-total)) "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<a id=\"task2\"></a>\n",
    "<h3> Task 2</h3>\n",
    "\n",
    "Remember Hadamard operator:\n",
    "\n",
    "$$\n",
    "    H = \\hadamard.\n",
    "$$\n",
    "\n",
    "Let's randomly create a 2-dimensional quantum state, and test whether Hadamard operator preserves the length or not.\n",
    "\n",
    "Write a function that returns a randomly created 2-dimensional quantum state:\n",
    "<ul>\n",
    "    <li> Pick a random value between 0 and 100 </li>\n",
    "    <li> Divide it by 100</li>\n",
    "    <li> Take sqaure root of it</li>\n",
    "    <li> Randomly determine its sign ($+$ or $-$)</li>\n",
    "    <li> This is the first entry of the vector </li>\n",
    "    <li> Find an appropriate value for the second entry </li>\n",
    "    <li> Randomly determine its sign ($+$ or $-$)</li>\n",
    "</ul>\n",
    "\n",
    "Write a function that determines whether a given vector is a valid quantum state or not.\n",
    "\n",
    "(Due to precision problem, the summation of squares may not be exactly 1 but very close to 1, e.g., 0.9999999999999998.)\n",
    "\n",
    "Repeat 10 times:\n",
    "<ul>\n",
    "    <li> Randomly create a quantum state </li>\n",
    "    <li> Multiply Hadamard matrix with the randomly created quantum state </li>\n",
    "    <li> Check whether the result quantum state is valid </li>\n",
    "</ul>"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "<h3> Solution </h3>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from random import randrange\n",
    "# randomly create a 2-dimensional quantum state\n",
    "def random_quantum_state():\n",
    "    first_entry = randrange(100)\n",
    "    first_entry = first_entry/100\n",
    "    first_entry = first_entry**0.5 # we found the first value before determining its sign\n",
    "    if randrange(2) == 0: # determine the sign\n",
    "        first_entry = -1 * first_entry\n",
    "    second_entry = 1 - (first_entry**2)\n",
    "    second_entry = second_entry**0.5\n",
    "    if randrange(2) == 0: # determine the sign\n",
    "        second_entry = -1 * second_entry\n",
    "    return [first_entry,second_entry]\n",
    "\n",
    "def is_quantum_state(quantum_state):\n",
    "    length_square = 0\n",
    "    for i in range(len(quantum_state)):\n",
    "        length_square += quantum_state[i]**2\n",
    "    print(\"summation of entry squares is\",length_square)\n",
    "    # there might be precision problem\n",
    "    # the length may be very close to 1 but not exactly 1\n",
    "    # so we use the following trick\n",
    "    if (length_square - 1)**2 < 0.00000001: return True \n",
    "    return False # else\n",
    "\n",
    "\n",
    "# define a function for Hadamard multiplication\n",
    "def hadamard(quantum_state):\n",
    "    result_quantum_state = [0,0] # define with zero entries\n",
    "    result_quantum_state[0] = (1/(2**0.5)) * quantum_state[0] + (1/(2**0.5)) * quantum_state[1]\n",
    "    result_quantum_state[1] = (1/(2**0.5)) * quantum_state[0] - (1/(2**0.5)) * quantum_state[1]\n",
    "    return result_quantum_state\n",
    "\n",
    "# we are ready\n",
    "for i in range(10):\n",
    "    picked_quantum_state=random_quantum_state()\n",
    "    print(picked_quantum_state,\"this is randomly picked quantum state\")\n",
    "    new_quantum_state = hadamard(picked_quantum_state)\n",
    "    print(new_quantum_state,\"this is new quantum state\")\n",
    "    print(\"Is it valid?\",is_quantum_state(new_quantum_state))\n",
    "    print() # print an empty line"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
